package testdatagenerator;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


//Is simply a wrapper for the String class that is useful for my purposes
//specifically it stores all the log data of the simulation to later be stored to a file
//and these strings will happen to be somewhat large. More specifically this class is
//for saving log data, to the file, about the simulation.
public class GiantString {
	private boolean cancel;
	private FileWriter fw;
	private File fFile;
	private static GiantString theString = null;
	private String toBeAppended;
	
	public static GiantString getGiantString(){
		if (theString==null){
			theString = new GiantString();
		}
		return theString;
	}
	
	private GiantString(){
		toBeAppended = "";
		cancel = false;
	}
	
	public void add(int x){
		addField(""+((Integer)x).toString()+"");
	}
	
	public void add(double x){
		addField(""+((Double)x).toString()+"");
	}
	
	public void add(char x){
		addField("\'"+x+"\'");
	}
	
	public void add(String s){
		addField("\'"+s+"\'");
	}
	
	public void add(Calendar x){
		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		addField("\'"+time.format(x.getTime())+"\'");
	}
	
	private void addField(String s){
		if (!toBeAppended.equalsIgnoreCase("")){
			toBeAppended += ",";
		}	
		toBeAppended += s;
	}
	
	public void Done(){
		toBeAppended += '\n';
		Save();
		toBeAppended = "";
	}
	
	private void getFile(){
		fFile = null;
		int response = JOptionPane.CANCEL_OPTION;
		JFileChooser choose = new JFileChooser();
		choose.setCurrentDirectory (new File("../.."));
		while(response==JOptionPane.CANCEL_OPTION){
			int result  = choose.showSaveDialog(new JButton("Export"));
			if (result == JFileChooser.CANCEL_OPTION) {
	         response = JOptionPane.OK_OPTION;
	         cancel = true;
			} else if (result == JFileChooser.APPROVE_OPTION) {
				fFile = choose.getSelectedFile ();
				if (fFile.exists ()) {
					response = JOptionPane.showConfirmDialog (null,
	               "Overwrite existing file?","Confirm Overwrite",
	                JOptionPane.OK_CANCEL_OPTION,
	                JOptionPane.QUESTION_MESSAGE); 
				}  else {
					response = JOptionPane.OK_OPTION;
				}
			}
		}	
		if (cancel){
			return;
		}
	    try{	
	        fFile.createNewFile();
	    }catch(Exception exc){
	        System.out.println("Error: Saving file failed!");
	    } 
	}
	
        // deprecated
	public void Start(){
//		
//		getFile();
//		if (cancel){
//			return;
//		}
//		if (fFile==null){
//			System.out.println("Error: Could not create file.");
//			return;
//		}
//		try {
//			fw = new FileWriter(fFile);
//		} catch(Exception exc){
//			System.out.println("Error: Could not write to file.");
//		}
	}
	//deprecated
	public void Save(){
//		if (cancel){
//			return;
//		}
//		try {
//			fw.write(toBeAppended);
//		} catch(Exception exc){
//			System.out.println("Could not print file peice.");
//		}
		
	}
        //deprecated 
	public void Finish(){
//		if (cancel){
//			return;
//		}
//		try {	
//			fw.flush();
//			fw.close();
//		} catch(Exception exc){
//			System.out.println("Error: Could not close file.");
//		}
	}
        
        public String toString(){
            return toBeAppended;
        }
}
